package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Opt;
import com.net.tkc.pro.entity.Post;
import com.net.tkc.pro.entity.PostImg;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.impl.PostImgImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/PostImg/")
public class PostImgController {
    ResultEntity resultEntity = null;
    @Resource
    PostImgImpl postImgImpl;

    /**
     * 删除帖子图片
     * @param id
     * @return
     */
    @DeleteMapping("PostImg")
    private String delImg(@RequestParam("id") int id){
        resultEntity = new ResultEntity();
        try{
            int result = postImgImpl.delImg(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 根据帖子id查看图片
     * @param postid
     * @return
     */
    @GetMapping("PostImg")
    private String getImgList(
            @RequestParam("postid") int postid
    ){
        resultEntity = new ResultEntity();
        try{
            List<PostImg> list =postImgImpl.getImgList(postid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看帖子id是否正确");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 添加postid图片
     * @param postid
     * @param img1
     * @param img2
     * @param img3
     * @param img4
     * @param img5
     * @param img6
     * @param img7
     * @param img8
     * @param img9
     * @return
     */
    @PostMapping("PostImg")
    private String addImg(
            @RequestParam("postid") int postid,
            @RequestParam("img1") String img1,
            @RequestParam("img2") String img2,
            @RequestParam("img3") String img3,
            @RequestParam("img4") String img4,
            @RequestParam("img5") String img5,
            @RequestParam("img6") String img6,
            @RequestParam("img7") String img7,
            @RequestParam("img8") String img8,
            @RequestParam("img9") String img9
    ){
        resultEntity = new ResultEntity();
        PostImg postImg=new PostImg();
        postImg.setPostid(postid);
        postImg.setImg1(img1);
        postImg.setImg2(img2);
        postImg.setImg3(img3);
        postImg.setImg4(img4);
        postImg.setImg5(img5);
        postImg.setImg6(img6);
        postImg.setImg7(img7);
        postImg.setImg8(img8);
        postImg.setImg9(img9);
        try{
            int result = postImgImpl.addImg(postImg);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
